package org.nox.helloseries.server.services.download.impl;

import java.io.IOException;
import java.net.SocketTimeoutException;

import org.nox.helloseries.domain.Serie;
import org.nox.helloseries.factory.HelloSeriesServiceFactory;
import org.nox.helloseries.server.exceptions.CookieException;
import org.nox.helloseries.server.remoting.transmission.TransmissionConnector;
import org.nox.helloseries.server.services.download.TorrentService;
import org.nox.helloseries.server.services.url.UrlService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class DefaultTorrentService implements TorrentService
{
    private static final Logger LOG = LoggerFactory.getLogger( DefaultTorrentService.class );

    private TransmissionConnector transmissionConnector = new TransmissionConnector();

    // Imports des services
    private UrlService urlService = HelloSeriesServiceFactory.createOrGetService( UrlService.class );

    @Override
    public boolean startDownload( Serie serie )
    {
        try
        {
            return transmissionConnector.send( urlService.downloadSerieToTemporaryFolder( serie ) );
        }
        catch ( SocketTimeoutException e )
        {
            LOG.error( "Timeout lors de la recherche du torrent", e );
            return false;
        }
        catch ( IOException | CookieException e )
        {
            LOG.error( "Erreur lors du téléchargement du fichier", e );
            return false;
        }
    }
}
